using Autofac;
using Hr.DAL;
using Hr.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hr.DALContainer
{
    public class Container
    {
        /// <summary>
        /// IOC 容器
        /// </summary>
        public static IContainer container = null;

        /// <summary>
        /// 获取 IDal 的实例化对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T Resolve<T>()
        {
            try
            {
                if (container == null)
                {
                    Initialise();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("IOC实例化出错！" + ex.Message);
            }
            return container.Resolve<T>();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private static void Initialise()
        {
            var builder = new ContainerBuilder();
            //格式：builder.RegisterType<xxxx>().As<Ixxxx>().InstancePerLifetimeScope();
            //用户列表
            builder.RegisterType<UserDAL>().As<IUser_ListDAL>().InstancePerLifetimeScope();
            // builder.RegisterType<Role_NameDAL>().As<IRole_NameDAL>().InstancePerLifetimeScope();
            //角色信息
            builder.RegisterType<RoleDAL>().As<IRole_NameDAL>().InstancePerLifetimeScope();
            //车辆信息
            builder.RegisterType<CarDAL>().As<ICar_ListDAL>().InstancePerLifetimeScope();
            //任务列表
            builder.RegisterType<TaskDAL>().As<ITask_ListDAL>().InstancePerLifetimeScope();
            //流程信息
            builder.RegisterType<ProcessDAL>().As<IProcess_ListDAL>().InstancePerLifetimeScope();
            //新闻信息
            builder.RegisterType<ArticleDAL>().As<IArticle_List>().InstancePerLifetimeScope();
            //简历信息
            //builder.RegisterType<ResumeListDAL>().As<IResume_ListDAL>().InstancePerLifetimeScope();

            //菜单管理
            builder.RegisterType<MenuDAL>().As<ISys_MenuDAL>().InstancePerLifetimeScope();
            //邮箱信息
            builder.RegisterType<MailDAL>().As<IMail_ListDAL>().InstancePerLifetimeScope();
            //公告信息
            builder.RegisterType<NoticeDAL>().As<INoticeDAL>().InstancePerLifetimeScope();
            container = builder.Build();
        }
    }
}
